package guo.redisson;

import com.sun.org.apache.regexp.internal.RE;
import guo.redisson.entity.PersonJson;
import guo.redisson.util.R;
import org.redisson.Redisson;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/redisson")
@ResponseBody
public class redissonTest {

    @Autowired
    RedissonClient redissonClient;


    @GetMapping("/setnx")
    public R testSetNx() throws InterruptedException {
        RLock rlock = null;
        try {
            rlock = redissonClient.getLock("testSetNx");
            rlock.lock(20,TimeUnit.SECONDS);
            Thread.sleep(15000);
            System.out.println("站锁成功");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            rlock.unlock();
        }
        return R.ok();
    }

    @GetMapping("/getnx")
    public R testSetNx2() throws InterruptedException {
        RLock rlock = null;
        try {
            rlock = redissonClient.getLock("testSetNx");
            rlock.lock(20, TimeUnit.SECONDS);
            Thread.sleep(35000);
            System.out.println("chhcchchhcch");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            rlock.unlock();
        }
        return R.ok();
    }

    @GetMapping("/writelock")
    public R writeSomeThing(){
        RReadWriteLock rrlock = null;
        RLock rlock = null;
        try {
            rrlock = redissonClient.getReadWriteLock("readwritelock");
            rlock = rrlock.writeLock();
            rlock.lock(20,TimeUnit.SECONDS);
            Thread.sleep(15000);
            System.out.println("写完了");
            return R.ok("写完了");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            rlock.unlock();
        }
    }

    @GetMapping("/readlock")
    public R readSomeThing(){
        RReadWriteLock rrlock = null;

        RLock rlock = null;

        try {
            rrlock = redissonClient.getReadWriteLock("readwritelock");
            rlock = rrlock.readLock();
            rlock.lock(20,TimeUnit.SECONDS);
            Thread.sleep(15000);
            System.out.println("读完完了");
            return R.ok("读完了");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            rlock.unlock();
        }
    }

    @GetMapping("/count")
    public R countDownLatchLock() throws InterruptedException {
        RCountDownLatch clock = null;
        try {
            clock = redissonClient.getCountDownLatch("count");
            clock.trySetCount(5);
            clock.await();
            System.out.println("5个资源都释放了");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return R.ok("5个资源都释放了");
    }

    @GetMapping("/countdown")
    public R counttDownLatchLock() throws InterruptedException {
        RCountDownLatch clock = null;
        try {
            clock = redissonClient.getCountDownLatch("count");
            Thread.sleep(5000);
            System.out.println("释放资源");
            clock.countDown();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return R.ok("资源释放了");
    }

    @GetMapping("/semaphore")
    public R SemaphoreLock() throws InterruptedException {
        RSemaphore semaphore = redissonClient.getSemaphore("semaphore");
        boolean b = semaphore.tryAcquire();
        if (b){
            System.out.println("得到信号量");
            Thread.sleep(20000);
            semaphore.release();
            return R.ok("得到");
        }else {
            System.out.println("没得到信号量");
            return R.ok("没得到");
        }
    }


    @GetMapping("/person")
    @Cacheable(value = {"getperson"})
    public R getPersonJson(){
        PersonJson personJson = new PersonJson();
        personJson.setAge(10);
        personJson.setName("you are very come");
        personJson.setNotSer("not");
        return R.ok().put("person",personJson);
    }

    @GetMapping("/delperson")
    @CacheEvict(value = {"getperson"},allEntries = true)
    public R delPersonJson(){

        Thread res = new Thread(() -> {
            System.out.println("runnable");
        });

        return R.ok();
    }
}